package com.mspbots.core;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.io.Serializable;

/**
 * @ClassName com.mspbots.core.CacheBaseServiceImpl
 * @Description TODO
 * @Author Jun
 * @Date 2019/10/11 13:57
 * @Version 1.0
 **/
public abstract class CacheBaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends AbstractBaseServiceImpl<M, T> {

    @CachePut(key = "#result.id", unless = "#result==null")
    public T insert(T t) {
        this.save(t);
        return t;
    }

    @CachePut(key = "#result.id", unless = "#result==null")
    public T update(T t) {
        this.updateById(t);
        return t;
    }

    @CacheEvict(key = "#id")
    public boolean remove(Serializable id) {
        return this.removeById(id);
    }

    @Cacheable(key = "#id")
    public T findById(Serializable id) {
        return this.getById(id);
    }

    @Cacheable(key = "#result.id", unless = "#result==null")
    public T find(T t) {
        return this.getOne(new QueryWrapper<>(t));
    }

}
